//
//  SelectCircleView.m
//  OrderSeat
//
//  Created by wangwei on 13-10-23.
//  Copyright (c) 2013年 wangwei. All rights reserved.
//

#import "SelectCircleView.h"
#import "DbManager.h"

#define AREA_TABLE_TAG 1001
#define CIRCLE_TABLE_TAG 1002

@implementation SelectCircleView


- (id)initWithFrame:(CGRect)frame
{
    if (SystemVersion < SystemVersion_7_0) frame.origin.y -= 64;
    self = [super initWithFrame:frame];
    if (self) {
        self.hidden = YES;
        self.viewHeight = 320;
        
        _dataSource = [[SelectCircleDataSource alloc] initWithView:self];
        
        _areaTable = [[UITableView alloc] initWithFrame:frame];
        _areaTable.top = 0;
        _areaTable.width = 120;
        _areaTable.tag = AREA_TABLE_TAG;
        _areaTable.dataSource = _dataSource;
        _areaTable.delegate = _dataSource;
        _areaTable.backgroundColor = [UIColor clearColor];
//        _areaTable.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
        
        [_areaTable drawBorder];
        
        [self addSubview:_areaTable];
        
        _circleTable = [[UITableView alloc] initWithFrame:frame];
        _circleTable.top = 0;
        _circleTable.width = 200;
        _circleTable.tag = CIRCLE_TABLE_TAG;
        _circleTable.left = _areaTable.right;
        _circleTable.dataSource = _dataSource;
        _circleTable.delegate = _dataSource;
//        _circleTable.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
        _circleTable.backgroundColor = [UIColor clearColor];
        [self addSubview:_circleTable];
        [_circleTable drawBorder];
        
        [self drawBorderWidth:1];
        
        self.backgroundColor = [UIView colorWithHexString:@"#F7F7F7"];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    _areaTable.height = self.height;
    _circleTable.height = self.height;
}

- (void)resetAttributesWithTop:(CGFloat)top
{
    self.height = self.hidden ? 0 : self.viewHeight;
    self.top = self.hidden ? (TOP_V7(top)-self.height) : TOP_V7(top);
}

- (void)showOrVisible
{
    [UIView animateWithDuration:0.4 animations:^(void) {
        self.hidden = !self.hidden;
        [self resetAttributesWithTop:46];
    }];
}

- (void)hidden
{
    [UIView animateWithDuration:0.4 animations:^(void) {
        self.hidden = YES;
        [self resetAttributesWithTop:46];
    }];
}

@end


//=============================================================================================
#pragma ===================数据源类实现===========================================================
@implementation SelectCircleDataSource


- (id)initWithView:(id)view
{
    self = [super init];
    if(self) {
        self.view = view;
        [self initAreas];
    }
    
    return self;
}

- (void)initAreas
{
    self.areaList = [[DbManager sharedManager] fetchAreas];
    [self loadCircleWithIndex:0];
}

#pragma ===================加载tableview数据 Begin================================================

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return tableView.tag == AREA_TABLE_TAG ? _areaList.count : _circleList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = tableView.tag == AREA_TABLE_TAG ? @"AreaCell" : @"CircleCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell setBackgroundColor:[UIColor clearColor]];
        cell.textLabel.textColor = [UIView colorWithHexString:@"#666666"];
        cell.textLabel.font  = [UIFont boldSystemFontOfSize:18];
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    }
    
    if (tableView.tag == AREA_TABLE_TAG) {
        NSDictionary *dic = [_areaList objectAtIndex:indexPath.row];
        cell.textLabel.text = [dic objectForKey:@"xianqu"];
        if (indexPath.row == 0 && !_view.isLoaded) {
            [_view.areaTable selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
            _view.isLoaded = YES;
        }
    }else {
        NSDictionary *dic = [_circleList objectAtIndex:indexPath.row];
        cell.textLabel.text = [dic objectForKey:@"lx_name"];
    }
    
    return cell;
}

#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == AREA_TABLE_TAG) {
        [self loadCircleWithIndex:indexPath.row];
        [self.view.circleTable reloadData];
    }
    if (tableView.tag == CIRCLE_TABLE_TAG) {
        [self.view.delegate selectCircleFinished:[_circleList objectAtIndex:indexPath.row]];
        [self.view hidden];
    }
}

- (void)loadCircleWithIndex:(NSInteger)index
{
    if(_areaList.count == 0) {
        self.circleList = [NSMutableArray array];
        return;
    }
    
    NSDictionary *dic = [_areaList objectAtIndex:index];
    NSString *areaId = [dic objectForKey:@"id"];
    self.circleList = [[DbManager sharedManager] fetchCirclesWithAreaId:areaId];
}

#pragma ===================加载tableview数据 End================================================

@end
